package com.woniuxy.conf;

import com.woniuxy.realm.UserRealm;
import org.apache.catalina.User;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author: 曾令忠
 * @date: 2020/7/3 10:07
 * @description:
 */
@Configuration
public class ShiroConfiguration {
    //realm
    @Bean
    public Realm realm(){
        return new UserRealm();
    }
    //securitymanager
    @Bean
    public SecurityManager securityManager(Realm realm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(realm);
        return securityManager;
    }
    //shiro filter
    @Bean
    public ShiroFilterFactoryBean factoryBean(SecurityManager securityManager){
        ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
        factoryBean.setSecurityManager(securityManager);
        //登录成功之后跳转的页面
        factoryBean.setSuccessUrl("/index.html");
        //登录页面
        factoryBean.setLoginUrl("/login.html");
        //过滤器链
        Map<String,String> map = new LinkedHashMap<>();//有序
        //哪些不需要认证
        map.put("/**","anon");
        map.put("/index.html","anon");
        map.put("/login.html","anon");
        map.put("/product.html","anon");

        map.put("/user/login","anon");
        map.put("/product/all","anon");
        map.put("/product/find","anon");

        //静态资源放行
        map.put("/html/**","anon");
        map.put("/css/**","anon");
        map.put("/js/**","anon");
        map.put("/images/**","anon");

        //认证
//        map.put("/**","authc");

        //设置过滤器链
        factoryBean.setFilterChainDefinitionMap(map);

        //
        return factoryBean;
    }
}
